package org.example.admin.remote;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.admin.common.convention.result.Result;
import org.example.admin.remote.dto.req.*;
import org.example.admin.remote.dto.resp.GroupShortLinkCountRespDTO;
import org.example.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import org.example.admin.remote.dto.resp.ShortLinkPageRespDTO;

import java.util.HashMap;
import java.util.List;

/**
 * 远程请求短链接project的服务类
 *
 * @author weibin
 * @since 2025/10/5 16:48
 */
public interface ShortLinkRemoteService {

    /**
     * 分页获取短链接
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam) {
        String responseStr = HttpUtil.post("127.0.0.1:8002/short-link/pageShortLink", JSON.toJSONString(requestParam));
        return JSON.parseObject(responseStr, new TypeReference<>() {});
    }

    /**
     * 创建短链接
     */
    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreatReqDTO requestParam) {
        String responseStr = HttpUtil.post("127.0.0.1:8002/short-link/createShortLink", JSON.toJSONString(requestParam));
        return JSON.parseObject(responseStr, new TypeReference<>() {});
    }

    /**
     * 查询分组内短链接数量
     */
    default Result<List<GroupShortLinkCountRespDTO>> listGroupShortLinkCount(List<String> requestParam) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("requestParam", requestParam);
        String responseStr = HttpUtil.get("127.0.0.1:8002/short-link/listGroupShortLinkCount", paramMap);
        return JSON.parseObject(responseStr, new TypeReference<>() {});
    }

    /**
     * 修改短链接
     */
    default Result<Void> updateShortLink(ShortLinkUpdateReqDTO requestParam) {
        String responseStr = HttpUtil.post("127.0.0.1:8002/short-link/updateShortLink", JSON.toJSONString(requestParam));
        return JSON.parseObject(responseStr, new TypeReference<>() {});
    }

    /**
     * 根据 url 获取网页标题
     */
    default Result<String> getUrlTitle(String url) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("url", url);
        String responseStr = HttpUtil.post("127.0.0.1:8002/short-link/urlTitle", paramMap);
        return JSON.parseObject(responseStr, new TypeReference<>() {});
    }

    /**
     * 将短链接移至回收站
     */
    default void moveTo(MoveToRecycleBinReqDTO requestParam) {
        HttpUtil.post("127.0.0.1:8002/recycle-bin/moveTo", JSON.toJSONString(requestParam));
    }

    /**
     * 将短链接从回收站恢复
     */
    default void recover(RecoverRecycleBinReqDTO requestParam) {
        HttpUtil.post("127.0.0.1:8002/recycle-bin/recover", JSON.toJSONString(requestParam));
    }

    /**
     * 删除短链接
     */
    default void delete(DeleteRecycleBinReqDTO requestParam) {
        HttpUtil.post("127.0.0.1:8002/recycle-bin/delete", JSON.toJSONString(requestParam));
    }
}
